#同步本地所有文件到服务器
import os,json
import requests

server=''
workPath=r""

toUpload=[]
m={}
total=0
def diff(files,end=False):
    global toUpload,m,total
    if len(files)>0:
        opt={'operation':'upload','objects':files}
        r=requests.post(server+"/objects/batch",json=opt,headers={'Content-Type':'application/vnd.git-lfs+json; charset=utf-8','Accept':'application/vnd.git-lfs+json'})
        res=json.loads(r.text)
        for i in res['objects']:
            if 'upload' in i['actions']:
                toUpload.append({'oid':i['oid'],'path':m[i['oid']]})
    if len(toUpload)>100 or (end and len(toUpload)>0):
        print('开始上传')
        for x in toUpload:
            uplaod(x)
        total+=len(toUpload)
        print(f'本次上传{len(toUpload)}个文件，总{total}个文件')
        toUpload.clear()

def uplaod(item):
    requests.put(server+"/objects/"+item['oid'],data=open(item['path'],'rb'),headers={'Accept':'application/vnd.git-lfs'})

def traverse_folder(path):
    s=0
    arr=[]
    for root, dirs, files in os.walk(path+'/.git/lfs/objects'):
        for file in files:
            if file.startswith('.'):
                continue
            name=os.path.join(root, file)
            size=os.path.getsize(name)
            arr.append({'oid':file,'size':size})
            m[file]=name
            s+=1
            if s%1000==0:
                diff(arr)
                arr.clear()
                m.clear()
    diff(arr,True)

# Example usage:
traverse_folder(workPath)